package com.lyt.springboot.util.test;

import java.util.concurrent.CompletableFuture;

public class ParallelSumService {
    AService aService = new AService();
    BService bService = new BService();
    CService cService = new CService();

    public int sumParallel() throws Exception {
        CompletableFuture<Integer> aFuture = CompletableFuture.supplyAsync(() -> aService.get());
        CompletableFuture<Integer> bFuture = CompletableFuture.supplyAsync(() -> bService.get());
        CompletableFuture<Integer> cFuture = CompletableFuture.supplyAsync(() -> cService.get());

        Integer a = aFuture.get();
        Integer b = bFuture.get();
        Integer c = cFuture.get();
        return a + b + c;
    }

    public static void main(String[] args) {
        ParallelSumService parallelSumService = new ParallelSumService();
        int i = 0;
        try {
            i = parallelSumService.sumParallel();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(i);
    }

    class AService {
        public int get() {
            return 10;
        }
    }

    class BService {
        public int get() {
            return 10;
        }
    }

    class CService {
        public int get() {
            return 10;
        }
    }
}